/**
 * @Author MrsGuo
 * version 1.0
 * @Date 2023-08-02 16:22
 */
package com.cl.erp_2301.controller;

import com.cl.erp_2301.Service.EmpService;
import com.cl.erp_2301.Service.LoginService;

import com.cl.erp_2301.entity.Emp;
import com.cl.erp_2301.util.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.HandlerInterceptor;

import java.io.IOException;

@RestController
@RequestMapping("/v2")
@Component
public class LoginController  {
    @Resource
    private LoginService loginService;
    @Resource
    private EmpService empService;
  @PostMapping("/login")
    public Result login(@RequestParam("username")String username, @RequestParam("pwd") String password, HttpServletRequest request, HttpServletResponse response) throws IOException {
       Emp emp=loginService.login(username, password);
       if (emp!=null){
           request.getSession().setAttribute("uuid",emp.getUuid());
           request.getSession().setAttribute("username",emp.getUsername());
           return Result.ok(emp.getUuid());
       }
       else{
           return  Result.error(401,"用户名，密码错误");
       }
    }

    @PostMapping("/login_showName")
    public Result showName(HttpServletRequest request,HttpServletResponse response){
       Integer id=(Integer)request.getSession().getAttribute("uuid");
       Emp emp= empService.getEmpById(id);
       request.getSession().setAttribute("name",emp.getName());
        return Result.rows(emp.getName());
    }


}
